package com.hbnu.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbnu.system.config.BaseException;
import com.hbnu.system.mapper.WorktypeMapper;
import com.hbnu.system.model.entity.Work_Type;
import com.hbnu.system.model.vo.ChildrenNameId;
import com.hbnu.system.model.vo.NameId;
import com.hbnu.system.service.IWorkTypeSerivce;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WorkTypeServiceImpl extends ServiceImpl<WorktypeMapper, Work_Type> implements IWorkTypeSerivce {

    @Override
    public boolean addWorkType(Work_Type work_type) {
        return false;
    }

    @Override
    public boolean deleteWorkType(String ID) {
        return false;
    }

    @Override
    public boolean updateWorkType(Work_Type work_type) {
        return false;
    }

    @Override
    public Work_Type checkOneWorkType(String ID) {
        Work_Type one = super.getOne(Wrappers.lambdaQuery(Work_Type.class)
                .eq(Work_Type::getWorkTypeID, ID));
        if (Objects.isNull(one)) {
            throw new BaseException("该类型数据不存在或ID有误!");
        }
        return one;
    }

    @Override
    public List<Work_Type> checkWorkTypes() {
        List<Work_Type> work_types = baseMapper.selectList(Wrappers.lambdaQuery(Work_Type.class));
        return work_types;
    }

    @Override
    public List<ChildrenNameId> getCascadingData() {
        List<ChildrenNameId> childrenNameIds = new ArrayList<>();

        HashMap<String,Integer> type1_s_map = new HashMap<>();
        HashMap<String,Integer> type2_s_1map = new HashMap<>();
        HashMap<String,Integer> type2_s_2map = new HashMap<>();

        //尝试一次性结束:
        List<Work_Type> work_types = baseMapper.selectList(Wrappers.lambdaQuery(Work_Type.class));

        //先把type1都取出 包括type1的name和id
        for (Work_Type work_type : work_types) {
            type1_s_map.put(work_type.getWorkType1(),work_type.getType1ID());
        }
//        type1_s_map.put("著作",1);
        // type1_s_map是有东西的

//        System.out.println(111);
        //再把type2也都取出来 包括type2的name和id
        for (Work_Type work_type : work_types) {
            if(work_type.getConnectionID() == 1){ // 用ConnectionID作为区分
                type2_s_1map.put(work_type.getWorkType2(),work_type.getType2ID());
            }else{
                type2_s_2map.put(work_type.getWorkType2(),work_type.getType2ID());
            }
        }

//        System.out.println(type2_s_1map);  // {译著=2, 其他=4, 专著=1, 编著=3}
//        System.out.println(type2_s_2map);  // {辑录=7, 改编、记谱=6, 其他=8, 创作=5}

//        System.out.println(type1_s_map);   {著作=1}
        //使用HashMap的For-Each迭代entries
        for (Map.Entry<String, Integer> entry : type1_s_map.entrySet()) {
            ChildrenNameId childrenNameId = new ChildrenNameId(entry.getKey(), entry.getValue());
            childrenNameIds.add(childrenNameId);
        }
//        System.out.println(childrenNameIds);

        for (ChildrenNameId childrenNameId : childrenNameIds) {
            if (childrenNameId.getValue() == 1){
                for (Map.Entry<String, Integer> entry : type2_s_1map.entrySet()) {
                    NameId nameId = new NameId(entry.getKey(), entry.getValue());
                    childrenNameId.getChildren().add(nameId);
                }
            }else {
                for (Map.Entry<String, Integer> entry : type2_s_2map.entrySet()) {
                    NameId nameId = new NameId(entry.getKey(), entry.getValue());
                    childrenNameId.getChildren().add(nameId);
                }
            }
        }
//        System.out.println(childrenNameIds);

        return childrenNameIds;

    }
}